
package com.wd.base.thread;

import android.os.Handler;

import androidx.annotation.Nullable;

import com.wd.base.util.Logger;

/**
 * <发送取消接口>
 *
 * @author wangnaiwen
 * @version [V1.0.0.0, 2020/5/23]
 * @since V1.0.0.0
 */
final class PostCancelable implements Cancelable {
    private static final String TAG = "PostCancelable";

    private final Runnable runnable;

    private final Handler handler;

    private volatile boolean canceled = false;

    private PostCancelable(@Nullable Handler h, @Nullable Runnable r) {
        this.handler = h;
        this.runnable = r;
    }

    static PostCancelable from(@Nullable Handler h, @Nullable Runnable r) {
        return new PostCancelable(h, r);
    }

    @Override
    public void cancel() {
        if (this.runnable != null && this.handler != null) {
            this.handler.removeCallbacks(this.runnable);
            this.canceled = true;
        } else {
            Logger.e(TAG, "runnable or handler not set, cannot cancel");
        }
    }
    @Override
    public boolean isCanceled() {
        return this.canceled;
    }
}
